package com.mdnote.jianzhi_offer.big_data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/10/23
 * @description 剑指 Offer 17. 打印从1到最大的n位数
 */
public class Offer17 {

    private char[] nums;

    private List<Integer> resList;

    public int[] printNumbers(int n) {
        char[] nums = new char[n];
        Arrays.fill(nums, '0');

        this.nums = nums;

        while (!insert()) {
            print();
        }
        return null;
    }

    private boolean insert() {
        boolean isOverflow = false;
        int nTakeOver = 0;

        for (int i = this.nums.length - 1; i >= 0 ; i--) {
            int nSum = this.nums[i] - '0' + nTakeOver;

            if (i == this.nums.length - 1) {
                // 个位 + 1
                nSum++;
            }
            if (nSum >= 10) {
                // 进位
                if (i == 0) {
                    // 已经溢出
                    return true;
                } else {
                    nSum -= 10;
                    this.nums[i] = (char) (nSum + '0');
                    nTakeOver = 1;
                }
            } else {

                this.nums[i] = (char) (nSum + '0');
                break;
            }
        }
        return isOverflow;
    }

    private void print() {
        int begin = 0;
        int index = 0;
        while (index < this.nums.length) {
            if (this.nums[index] != '0') {
                begin = index;
                break;
            }
            index++;
        }
        System.out.println(new String(this.nums).substring(begin));
    }

    public static void main(String[] args) {
        Offer17 offer17 = new Offer17();
        int[] ints = offer17.printNumbers(2);
    }
}
